use 4_conditionalupdatingmeasures.dta, clear
format date %tq
tsset date 
tsfill
sort date
gen month=month(dofq(date))
gen year=year(dofq(date))

gen twoyearcycle = floor((year-1953)/2)+1

sort twoyearcycle
by twoyearcycle: gen counter=_n

sort date
gen ics = abs(partyics)
gen begics = ics if counter==1
gen endics = ics if counter==8

gen presapprove = abs(partyapprove)
gen begapprove = presapprove if counter==1
gen endapprove = presapprove if counter==8

gen begmacro = ypred-ytilde if counter==1
gen endmacro = ypred-ytilde if counter==8


gen diff = ypred - ytilde
sort twoyearcycle
by twoyearcycle: egen sdypred = sd(ypred)
by twoyearcycle: egen sddiff = sd(diff)


** place a year behind to correspond with the forward House measures
gen netpartyswing = -22 if year==1951
replace netpartyswing = 19 if year==1953
replace netpartyswing = 2 if year==1955
replace netpartyswing = 49 if year==1957
replace netpartyswing = -22 if year==1959
replace netpartyswing = -1 if year==1961
replace netpartyswing = 37 if year==1963
replace netpartyswing = -47 if year==1965
replace netpartyswing = -5 if year==1967
replace netpartyswing = 12 if year==1969
replace netpartyswing = -12 if year==1971
replace netpartyswing = 49 if year==1973
replace netpartyswing = 1 if year==1975
replace netpartyswing = -15 if year==1977
replace netpartyswing = -34 if year==1979
replace netpartyswing = 26 if year==1981
replace netpartyswing = -14 if year==1983
replace netpartyswing = 5 if year==1985
replace netpartyswing = 2 if year==1987
replace netpartyswing = 9 if year==1989
replace netpartyswing = -10 if year==1991
replace netpartyswing = -52 if year==1993
replace netpartyswing = 3 if year==1995
replace netpartyswing = 5 if year==1997
replace netpartyswing = 2 if year==1999
replace netpartyswing = -8 if year==2001
replace netpartyswing = -3 if year==2003
replace netpartyswing = 31 if year==2005
replace netpartyswing = 21 if year==2007
replace netpartyswing = -63 if year==2009
replace netpartyswing = 8 if year==2011

gen percentreturning = 81.4 if year==1951
replace percentreturning = 87.1 if year==1953
replace percentreturning = 89.4 if year==1955
replace percentreturning = 81.8 if year==1957
replace percentreturning = 86.2 if year==1959
replace percentreturning = 84.6 if year==1961
replace percentreturning = 79.1 if year==1963
replace percentreturning = 83.2 if year==1965
replace percentreturning = 91.0 if year==1967
replace percentreturning = 87.1 if year==1969
replace percentreturning = 83.9 if year==1971
replace percentreturning = 78.9 if year==1973
replace percentreturning = 84.6 if year==1975
replace percentreturning = 82.3 if year==1977
replace percentreturning = 83.0 if year==1979
replace percentreturning = 81.4 if year==1981
replace percentreturning = 90.1 if year==1983
replace percentreturning = 88.5 if year==1985
replace percentreturning = 92.4 if year==1987
replace percentreturning = 89.7 if year==1989
replace percentreturning = 74.7 if year==1991
replace percentreturning = 80.0 if year==1993
replace percentreturning = 83.0 if year==1995
replace percentreturning = 90.1 if year==1997
replace percentreturning = 90.1 if year==1999
replace percentreturning = 88.0 if year==2001
replace percentreturning = 90.8 if year==2003
replace percentreturning = 87.1 if year==2005
replace percentreturning = 86.4 if year==2007
replace percentreturning = 91.3 if year==2009
replace percentreturning = 80.7 if year==2011

** again year behind to correspond with fpartyagenda/ideo
gen partyswitch = 0
replace partyswitch=1 if year>=1953 & year<=1954
replace partyswitch=1 if year>=1993 & year<=1994
replace partyswitch=1 if year>=2005 & year<=2006
replace partyswitch=1 if year>=2009 & year<=2010

gen stateswitch = 0
replace stateswitch = 2 if year>=1953 & year<=1954
replace stateswitch = 9 if year>=1957 & year<=1958
replace stateswitch = 2 if year>=1959 & year<=1960
replace stateswitch = 1 if year>=1961 & year<=1962
replace stateswitch = 7 if year>=1963 & year<=1964
replace stateswitch = 4 if year>=1965 & year<=1966
replace stateswitch = 6 if year>=1973 & year<=1974
replace stateswitch = 1 if year>=1975 & year<=1976
replace stateswitch = 3 if year>=1977 & year<=1978
replace stateswitch = 3 if year>=1979 & year<=1980
replace stateswitch = 3 if year>=1981 & year<=1982
replace stateswitch = 2 if year>=1983 & year<=1984
replace stateswitch = 1 if year>=1987 & year<=1988
replace stateswitch = 2 if year>=1991 & year<=1992
replace stateswitch = 7 if year>=1993 & year<=1994
replace stateswitch = 1 if year>=1995 & year<=1996
replace stateswitch = 1 if year>=1999 & year<=2000
replace stateswitch = 1 if year>=2001 & year<=2002
replace stateswitch = 1 if year>=2003 & year<=2004
replace stateswitch = 3 if year>=2005 & year<=2006
replace stateswitch = 3 if year>=2007 & year<=2008
replace stateswitch = 8 if year>=2009 & year<=2010
replace stateswitch = 4 if year>=2011 & year<=2012


gen supermajority = 0
replace supermajority = 1 if year>=1957 & year<=1958
replace supermajority = 1 if year>=1963 & year<=1964
replace supermajority = 1 if year>=1975 & year<=1976
replace supermajority = 1 if year>=2007 & year<=2008


sort twoyearcycle
by twoyearcycle: egen meanbegics = mean(begics)
by twoyearcycle: egen meanendics = mean(endics)
by twoyearcycle: egen meanbegapprove = mean(begapprove)
by twoyearcycle: egen meanendapprove = mean(endapprove)
by twoyearcycle: egen meanbegmacro = mean(begmacro)
by twoyearcycle: egen meanendmacro = mean(endmacro)


gen diffics = abs(meanendics-meanbegics)
gen diffapprove = abs(meanendapprove-meanbegapprove)
gen netdiffmacro = (meanendmacro-meanbegmacro)*100
gen diffmacro = abs(meanendmacro -meanbegmacro)*100

sort date
gen preselect = 0
replace preselect = 1 if twoyearcycle/2 == floor(twoyearcycle/2)

drop if counter!=1

gen abspartyswing = abs(netpartyswing)

** Table 2 results

reg avgfsd fpartyideochange 
reg avgfsd fpartyagendachange 
reg avgfsd fpartyideochange fpartyagendachange 
estat szroeter, rhs 
estat szroeter, rhs mtest(b)
reg avgfsd fpartyideochange fpartyagendachange [aw=avgfsdtau] 
set seed 2011
reg avgfsd fpartyideochange fpartyagendachange, vce(bootstrap, level(90) reps(1000))
estat bootstrap


*************************
** Test for endogeneity
*************************

** correlation is low

pwcorr diffmacro avgfsd, sig

** show seat swings not a function of updating
reg netpartyswing netdiffmacro
reg netpartyswing avgfsd
reg abspartyswing diffmacro
reg abspartyswing avgfsd


** Show that changes in macro don't produce changes in party conflict
** select instruments
reg avgfsd abspartyswing preselect percentreturning super 
** f is insignificant


reg fpartyideochange abspartyswing preselect percentreturning
predict test, resid
reg avgfsd fpartyideochange test
test test
drop test

reg fpartyagendachange abspartyswing percentreturning supermaj
predict test, resid
reg avgfsd fpartyagendachange test
test test
drop test


** Generate partial residuals for AV plots (Figure 
reg avgfsd fpartyideochange fpartyagendachange diffics diffapprove

reg fpartyideochange fpartyagendachange diffics diffapprove
predict ideology, resid
reg fpartyagendachange fpartyideochange diffics diffapprove
predict agenda, resid
reg avgfsd fpartyideochange diffics diffapprove
predict yagenda, resid
reg avgfsd fpartyagendachange diffics diffapprove
predict yideology, resid
*scatter yideology ideology
*scatter yagenda agenda
save "avplots.dta" , replace
